<?php
//
namespace app\circle\controller\admin;

use app\circle\model\AdminModel;
use app\circle\model\DeptModel;
use think\Request;

class AdminController extends BaseController
{

    private $deptModel;
    private $adminModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->adminModel = new AdminModel();
        $this->deptModel = new DeptModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        if($this->adminInfo['admin_power']==2){
            $map['dept_id'] = $this->adminInfo['dept_id'];
            $map['admin_power']=['in',[2,3]];
        }
        if($this->adminInfo['admin_power']==3){
            $map['admin_id'] = $this->adminInfo['admin_id'];
            $map['admin_power']=3;
        }
        $query      = $this->request->request();
        $searchKey  = $this->request->param('searchKey');
        $searchText = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey] = ['like', '%' . $searchText . '%'];
        }
        $list = $this->adminModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加管理员
     * @return mixed
     */
    public function add(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'admin_name|用户名'  => 'require|unique:circle_admin',
                'admin_pass|用户密码' => 'require',
                'admin_truename|真实姓名' => 'require',
                'dept_id|部门' => 'number|>:0',
                'admin_power|管理员权限' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $deptInfo = $this->deptModel->where('dept_id',$data['dept_id'])->find();
            if(empty($deptInfo)){
                $this->error('对应部门不存在！');
            }
            $data['dept_name'] = $deptInfo['dept_name'];
            $data['dept_path'] = $deptInfo['dept_path'];
            $data['dept_level'] = $deptInfo['dept_level'];
            $this->adminModel->addData($data);
            $this->success('操作成功！','index');
        }
        $this->assign('deptList',$this->deptModel->getTree());
        return $this->fetch();
    }

    /**
     * 编辑管理员
     */
    public function edit(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'admin_truename|真实姓名' => 'require',
                'admin_power|管理员权限' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            if(empty($data['admin_pass'])){
                unset($data['admin_pass']);
            }
            $this->adminModel->editData($data);
            $this->success('操作成功！','index');
        }
        $adminId = $this->request->param('admin_id/d');
        $info = $this->adminModel->where('admin_id',$adminId)->find();
        $this->assign('info',$info);
        $this->assign('deptList',$this->deptModel->getTree());
        return $this->fetch();
    }

    /**
     * 删除管理员
     */
    public function del(){
        $adminId = $this->request->param('admin_id/d');
        $this->adminModel->where('admin_id',$adminId)->delete();
        $this->success('删除成功！');
    }

    /**
     * 修改管理员密码
     */
    public function editpass(){
        if ($this->request->isAjax()) {
            $oldPassword     = $this->request->param('old_password');
            $newPassword     = $this->request->param('new_password');
            $replayPassword  = $this->request->param('replay_password');
            $map['admin_id']      = $this->adminInfo['admin_id'];
            $map['password'] = md5($oldPassword);
            if ($this->adminModel->where($map)->find()) {
                if ($newPassword == $replayPassword) {
                    $data['admin_id']      = $this->adminInfo['admin_id'];;
                    $data['password'] = $newPassword;
                    $this->adminModel->editData($data);
                    cookie('circleDes', null);
                    $this->success('密码修改成功,请重新登陆！', url('editpass'));
                } else {
                    $this->error('原密码和确认密码不一致！');
                }
            } else {
                $this->error('原密码输入错误！');
            }

        }
        $info = $this->adminModel->where('admin_id',$this->adminInfo['admin_id'])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

}
